<?php

declare(strict_types=1);

namespace app\controller\api;


use app\util\Sms;
use think\facade\Db;
use think\Response;
use app\util\WechatService;

/**
 * 驾校教练端
 */
class JxCoach extends Base
{

    //获取验证码
    public function getPhoneCode(): Response
    {
        $sms = new Sms();
        $param = $this->request->param();
        $phone = $param['phone']; //手机号
        if (empty($phone)) {
            return $this->buildFailed(400, '参数不正确');
        }
        $code = rand(1000, 9999);
        //发送短信
        $sems_data = Db::table('jx_sms')->where('phone', $phone)->find();
        if ($sems_data) {
            Db::table('jx_sms')->where('phone', $phone)->delete();
        }
        $data['phone'] = $phone;
        $data['code'] = $code;
        $data['addtime'] = date('Y-m-d H:i:s', time());
        $id = Db::table('jx_sms')->save($data);
        $resB = $sms->sendSms($phone, $code);
        return $this->buildSuccess([]);
    }

    //教练用户登录
    public function login(): Response
    {
        $param = $this->request->param();
        $code  = $param['code']; //微信code
        if (empty($code)) {
            return $this->buildFailed(400, 'code不能为空');
        }
        $wechat = new WechatService;
        $result = $wechat->wechatXcx()->auth->session($code);
        if (!empty($result['openid'])) {
            $openid = $result['openid'];
        } else {
            return $this->buildFailed(401, 'code错误');
        }
        //判断手机好验证码是否正确
        $row = Db::name('jx_sms')->where('phone', $param['phone'])->find();
        if ($row['code'] != $param['yzm']) {
            return $this->buildFailed(402, 'yzm错误');
        }

        //已注册！直接验证账号信息进行登录
        $map['user_phone'] = $param['phone'];
        $map['open_id'] = $openid;
        $map['status'] = array('in', '1,2');
        $userData = Db::name('jx_coach')->where($map)->find();
        if (empty($userData)) {
            $add['nickname'] = '微信用户' . rand(1, 999999); //用户昵称
            $add['user_img'] = "https://" . $_SERVER['SERVER_NAME'] . "/public/images/user_img.png"; //用户头像
            $add['open_id'] = $openid; //微信openID
            $add['add_time'] = date("Y-m-d H:i:s"); //注册时间
            $add['user_phone'] = $param['phone'];
            $add['pass_status'] = 1; //未审核
            $add['is_perfect'] = 0; //未完善
            $add['id'] = Db::name('jx_coach')->insertGetId($add);
            $userData = $add;
        } else {
            if ($userData['status'] == 2) {
                return $this->buildFailed(403, '账号被禁用');
            }
        }
        return $this->buildSuccess(['result' => $userData]);
    }

    //平台设置教练服务协议
    public function getCoachAgreement(): Response
    {
        $param = $this->request->param();
        $row = Db::table('jx_system')->where('mark', 'coach_agreement')->field('content')->find();
        return $this->buildSuccess($row);
    }

    //选择驾校
    public function selectSchool(): Response
    {
        $param = $this->request->param();
        if (isset($param['school_name']) && $param['school_name'] != '') {
            $where[] = ['school_name', 'like', $param['school_name'] . '%'];
        }
        $page = isset($param['page']) && ($param['page'] != '') ? $param['page'] : 1;
        $limit =  isset($param['limit']) && ($param['limit'] != '') ? $param['limit'] : 10;
        $where[] = ['status', 'in', ['1', '2']];
        $field = 'id,school_image,shcool_name';
        $count = Db::table('jx_school')->where($where)->count();
        $list = Db::table('jx_school')->where($where)->field($field)->page((int)$page, (int)$limit)->select()->toArray();
        $data['count'] = $count;
        $data['list'] = $list;
        return $this->buildSuccess($data);
    }

    //选择驾校场地
    public function selectSchoolState(): Response
    {
        $param = $this->request->param();
        if (isset($param['state_name']) && $param['state_name'] != '') {
            $where[] = ['state_name', 'like', $param['state_name'] . '%'];
        }
        $page = isset($param['page']) && ($param['page'] != '') ? $param['page'] : 1;
        $limit =  isset($param['limit']) && ($param['limit'] != '') ? $param['limit'] : 10;
        $where[] = ['status', 'in', ['1', '2']];
        $field = 'id,state_image,state_name';
        $count = Db::table('jx_school_state')->where($where)->count();
        $list = Db::table('jx_school_state')->where($where)->field($field)->page((int)$page, (int)$limit)->select()->toArray();
        $data['count'] = $count;
        $data['list'] = $list;
        return $this->buildSuccess($data);
    }

    //完善教练资料
    public function perfectCoach(): Response
    {
        $param = $this->request->param();
        $coach_id = $param['coach_id']; //教练用户id
        if ($coach_id == '') {
            return $this->buildFailed(400, '参数错误 教练用户id为空');
        }
        if (isset($param['user_name']) && $param['user_name'] != '') {
            $map['user_name'] = $param['user_name']; //用户姓名
        }
        if (isset($param['Id_card']) && $param['Id_card'] != '') {
            $map['Id_card'] = $param['Id_card'];  //用户身份证号
        }

        if (isset($param['user_phone']) && $param['user_phone'] != '') {
            $map['user_phone'] = $param['user_phone'];  //用户手机号
        }

        if (isset($param['school_id']) && $param['school_id'] != '') {
            $map['school_id'] = $param['school_id'];  //驾校id
        }

        if (isset($param['address']) && $param['address'] != '') {
            $map['address'] = $param['address'];  //家庭地址
        }

        if (isset($param['Id_front_image']) && $param['Id_front_image'] != '') {
            $map['Id_front_image'] = $param['Id_front_image'];  //身份证正面
        }

        if (isset($param['Id_back_image']) && $param['Id_back_image'] != '') {
            $map['Id_back_image'] = $param['Id_back_image'];  //身份证反面
        }

        if (isset($param['user_img']) && $param['user_img'] != '') {
            $map['user_img'] = $param['user_img'];  //真人头像及照片
        }

        if (isset($param['jiakao_type']) && $param['jiakao_type'] != '') {
            $map['jiakao_type'] = $param['jiakao_type'];  //驾考类型
        }

        if (isset($param['car_number']) && $param['car_number'] != '') {
            $map['car_number'] = $param['car_number'];  //车牌号码
        }

        if (isset($param['train_address']) && $param['train_address'] != '') {
            $map['train_address'] = $param['train_address'];  //训练场地id
        }

        if (isset($param['professional_certificate_image']) && $param['professional_certificate_image'] != '') {
            $map['professional_certificate_image'] = $param['professional_certificate_image'];  //从业资格证
        }


        if (isset($param['vehicle_policy_image']) && $param['vehicle_policy_image'] != '') {
            $map['vehicle_policy_image'] = $param['vehicle_policy_image'];  //车辆保单
        }
        if (isset($param['drivers_front_image']) && $param['drivers_front_image'] != '') {
            $map['drivers_front_image'] = $param['drivers_front_image'];  //驾驶证正面照片
        }

        if (isset($param['drivers_back_image']) && $param['drivers_back_image'] != '') {
            $map['drivers_back_image'] = $param['drivers_back_image'];  //驾驶证反面面照片
        }

        if (isset($param['run_front_image']) && $param['run_front_image'] != '') {
            $map['run_front_image'] = $param['run_front_image'];  //行驶证正面照片
        }

        if (isset($param['run_back_image']) && $param['run_back_image'] != '') {
            $map['run_back_image'] = $param['run_back_image'];  //行驶证正面照片
        }
        $map['is_perfect'] = 1; //完善资料
        $res = Db::table('jx_coach')->where('id', $coach_id)->save($map);
        return $this->buildSuccess();
    }

    //公告列表
    public function coachProclamationList(): Response
    {
        $param = $this->request->param();

        $page = isset($param['page']) && ($param['page'] != '') ? $param['page'] : 1;
        $limit =  isset($param['limit']) && ($param['limit'] != '') ? $param['limit'] : 10;
        $where['status'] = array('in', '1,2');
        if (isset($param['title']) && $param['title'] != '') {
            $where[] = ['title', 'like', '%' . $param['title'] . '%'];
        }

        $list = Db::table('jx_proclamation')->page((int)$page, (int)$limit)->where($where)->order('id desc')->select();
        $count = Db::table('jx_proclamation')->where($where)->count();
        $data['list'] = $list;
        $data['count'] = $count;
        if ($data) {
            return $this->buildSuccess($data);
        } else {
            return $this->buildFailed(401, '获取数据失败');
        }
    }

    //公告列表详情
    public function coachProclamationDetail(): Response
    {
        $param = $this->request->param();
        if (isset($param['id']) && $param['id'] == '') {
            return $this->buildFailed(400, 'id 参数错误');
        }
        $data = Db::table('jx_proclamation')->where('id', $param['id'])->find();
        if ($data) {
            return $this->buildSuccess($data);
        } else {
            return $this->buildFailed(401, '获取数据失败');
        }
    }

    //获取驾考类型
    public function getJiakaoTypeName(): Response
    {

        $data = Db::table('jx_jiakao_type_name')->select()->toArray();
        if ($data) {
            return $this->buildSuccess($data);
        } else {
            return $this->buildFailed(401, '获取数据失败');
        }
    }

    //发布二手车
    public function releaseCar(): Response
    {
        $param = $this->request->param();
        $coach_id = $param['coach_id']; //教练用户id

        if (isset($param['title']) && $param['title'] != '') {
            $map['title'] = $param['title'];  //车辆标题
        }

        if (isset($param['car_brand_id']) && $param['car_brand_id'] != '') {
            $map['car_brand_id'] = $param['car_brand_id'];  //车辆品牌id
        }
        if (isset($param['car_age']) && $param['car_age'] != '') {
            $map['car_age'] = $param['car_age'];  //车辆年龄
        }
        if (isset($param['car_price']) && $param['car_price'] != '') {
            $map['car_price'] = $param['car_price'];  //车辆价格
        }

        if (isset($param['mileage_number']) && $param['mileage_number'] != '') {
            $map['mileage_number'] = $param['mileage_number'];  //里程数
        }

        if (isset($param['car_image']) && $param['car_image'] != '') {
            $map['car_image'] = $param['car_image'];  //多张图片用，号隔开
        }

        if (isset($param['driving_image']) && $param['driving_image'] != '') {
            $map['driving_image'] = $param['driving_image'];  //行驶证照片
        }
        $map['coach_id'] = $coach_id;
        $map['add_time'] = date('Y-m-d H:i:s', time());

        if (isset($param['id']) && $param['id'] != '') {
            //修改
            $res = Db::name('jx_car')->where('id', $param['id'])->save($map);
        } else {
            //添加
            $res = Db::table('jx_car')->insert($map);
        }

        if ($res) {
            return $this->buildSuccess();
        } else {
            return $this->buildFailed();
        }
    }

    //二手车详情
    public function CarDetail(): Response
    {
        $param = $this->request->param();
        if (isset($param['id']) && $param['id'] == '') {
            return $this->buildFailed(400, 'id 参数错误');
        }
        $field  = 'c.*,co.user_name,co.nickname,co.user_img,co.user_phone';
        $data = Db::table('jx_car')->alias('c')->join('jx_coach co', 'co.id=c.coach_id')->field($field)->where('c.id', $param['id'])->find();
        if ($data['car_image']) {
            $data['car_image'] = explode(',', $data['car_image']);
        } else {
            $data['car_image'] = [];
        };
        if ($data['driving_image']) {
            $data['driving_image'] = explode(',', $data['driving_image']);
        } else {
            $data['driving_image'] = [];
        }
        //获取车主新


        if ($data) {
            return $this->buildSuccess($data);
        } else {
            return $this->buildFailed(401, '获取数据失败');
        }
    }

    //上下架二手车
    public function setUpperStatus(): Response
    {
        $param = $this->request->param();
        if (isset($param['id']) && $param['id'] == '') {
            return $this->buildFailed(400, 'id 参数错误');
        }
        $upper_status = isset($param['upper_status']) && ($param['upper_status'] != '') ? $param['upper_status'] : 1;
        $map['upper_status'] = $upper_status;
        $map['update_time'] = date('Y-m-d H:i:s', time());
        $res = Db::name('jx_car')->where('id', $param['id'])->save($map);
        if ($res) {
            return $this->buildSuccess();
        } else {
            return $this->buildFailed();
        }
    }
}
